package com.shanzhi.dify.service.impl;

import com.shanzhi.connection.common.HttpRequestType;
import com.shanzhi.dify.common.DifyUrlEnums;
import com.shanzhi.dify.entity.AgentEntity;
import com.shanzhi.dify.entity.WorkFlowsRunEntity;
import com.shanzhi.dify.service.DifyApiService;
import com.shanzhi.dify.service.DifyBaseService;
import com.shanzhi.dify.utils.RebuildParamsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * dify相关的接口：工作流、Agent等
 */
@Service
@Slf4j
public class DifyApiServiceImpl implements DifyApiService {

    @Autowired
    private DifyBaseService difyBaseService;


    /**
     * 工作流执行接口
     *
     * @param workFlowsRunEntity
     * @param apiKey
     * @return
     */
    @Override
    public String workflowsRun(WorkFlowsRunEntity workFlowsRunEntity, String apiKey, DifyUrlEnums difyUrlEnums) {
        return difyBaseService.sendDifyRequest(workFlowsRunEntity, apiKey, difyUrlEnums, HttpRequestType.POST, null);
    }



//    /**
//     * 工作流执行接口
//     *
//     * @param workFlowsRunEntity
//     * @param apiKey
//     * @return
//     */
//    @Override
//    public String workflowsRun(WorkFlowsRunEntity workFlowsRunEntity, String apiKey) {
//        return difyBaseService.sendDifyRequest(workFlowsRunEntity, apiKey, DifyUrlEnums.WORKFLOWS_RUN_URL, HttpRequestType.POST, null);
//    }

//    @Override
//    public String getTableStructure(WorkFlowsRunEntity workFlowsRunEntity, String apiKey) {
//        String url = difyBaseService.getRealUrl(DifyUrlEnums.WORKFLOWS_RUN_URL);
//        log.info("请求路径：{}", url);
//        return DifyCommonUtils.sendDifyForPost(JSONObject.toJSONString(workFlowsRunEntity), apiKey, url);
//    }


    /**
     * 生成echarts
     *
     * @param workFlowsRunEntity
     * @param apiKey
     * @return
     */
    @Override
    public void queryDataBase(WorkFlowsRunEntity workFlowsRunEntity, String apiKey) {
        difyBaseService.sendDifyRequest(workFlowsRunEntity, apiKey, DifyUrlEnums.CHAT_FLOW_URL, HttpRequestType.POST, null);
    }


    @Override
    public String chatFlow(WorkFlowsRunEntity workFlowsRunEntity, String apiKey) {
        return difyBaseService.sendDifyRequest(workFlowsRunEntity, apiKey, DifyUrlEnums.CHAT_FLOW_URL, HttpRequestType.POST, null);
    }


    /**
     * Agent模式不能使用Blocking 模式，只能使用非阻塞模式
     *
     * @param agentEntity
     * @return
     */
    @Override
    public String agentChat(AgentEntity agentEntity) {
        RebuildParamsUtils.rebuildAgentEntity(agentEntity);
        return difyBaseService.sendDifyRequest(agentEntity.getWorkFlowsRunEntity(), agentEntity.getApiKey(), DifyUrlEnums.CHAT_FLOW_URL, HttpRequestType.POST, null);
    }

}
